package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wf.captcha.SpecCaptcha;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    SystemUserMapper systemUserMapper;

    @Override
    public CaptchaVo getCaptcha() {
        //生成specCaptcha验证码
        SpecCaptcha specCaptcha = new SpecCaptcha(130,48,4);
        String text = specCaptcha.text().toLowerCase();
        String img = specCaptcha.toBase64();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX+ UUID.randomUUID().toString().replace("-","");

        //将key和text存入redis中

        redisTemplate.opsForValue().set(key, text, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

        //将key和img存入CaptchaVo中返回给前端
        CaptchaVo captchaVo = new CaptchaVo(img,key);

        return captchaVo;
    }

    @Override
    public String login(LoginVo loginVo) {
        //获取loginVo参数
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();
        String captchaKey = loginVo.getCaptchaKey();
        String captchaCode = loginVo.getCaptchaCode();

        //获取校验码、断言校验码
        String captchaCodeCache = redisTemplate.opsForValue().get(captchaKey);
        Assert.isTrue(StringUtils.hasText(captchaCodeCache)&&captchaCodeCache.equalsIgnoreCase(captchaCode),"验证码不正确！");

        //校验用户名和密码
        QueryWrapper<SystemUser> systemUserQueryWrapper = new QueryWrapper<>();
        systemUserQueryWrapper.eq("username",username);
        SystemUser systemUser = systemUserMapper.selectOne(systemUserQueryWrapper);
        Assert.notNull(systemUser,"该用户不存在！");

        String passworDb = systemUser.getPassword();
        String passwordVo = DigestUtils.md5Hex(password.getBytes());
        Assert.isTrue(passworDb.equals(passwordVo),"密码错误！");

        //生成token
        String token = JwtUtil.createToken(systemUser.getId(), systemUser.getUsername());

        return token;
    }

    @Override
    public SystemUserInfoVo info(String token) {


        //解析token
        Claims claims = JwtUtil.parseToken(token);
        Long userId = claims.get("userId",Long.class);
        Assert.notNull(userId,"Token解析失败");

        //查询数据库
        SystemUser systemUser = systemUserMapper.selectById(userId);

        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setName(systemUser.getName());
        systemUserInfoVo.setAvatarUrl(systemUser.getAvatarUrl());

        return systemUserInfoVo;
    }
}
